/**
 * 城市添加
 */
$(function($) {
	var entityID = getQueryString('id');
	if(entityID) {
		$.post(contextPath + "/staff/getStaff", {"id": entityID}, function(result) {
			$("#form-add").JsonToForm(result);
		});
	}

	getScenicSpotSelect();

	
	$("#form-add").validate({
		rules: {
			scenicSpotId:{
				required:true,
			},
			loginname:{
				required:true,
				maxlength:11,
			},
			loginpassword:{
				required:true,
				maxlength:20,
			},
			reloginpassword:{
				required:true,
				maxlength:20,
			}
		},
		onkeyup: function(element){
			$(element).valid();
		},
		submitHandler: function(form) {
			$.ajax({
				type: "post",
				url: contextPath + "/scenicSpot/update",
				data: $(form).serialize(),
				dataType: "json",
				success: function(data) {
					if(data.status == 200) {
					var message =  '更新成功!';
					var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引,隐藏layer层和shade
					parent.$('#layui-layer' + index).css({'display': 'none'});
					parent.$('#layui-layer-shade' + index).css({'display': 'none'});
					parent.reloadTable(); //再刷新DT
					parent.showSuccessMessage(message, null, function() {
						parent.layer.close(index); //然后执行关闭
					});
				} else {
					var message = '更新失败!';
			parent.showFailMessage(message);
		}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
					alert(errorThrown);
					return false;
				}
			});
			return false;
		}
	});

});



function getScenicSpotSelect(){
	jQuery.ajax({
		type:"POST",
		dataType:"json",
		url:contextPath + "/staff/getScenicSpotSelect",
		success:function(data){
			var unitObj=document.getElementById("selectList");
			if(data != null){
				//alert(JSON.stringify(selectList));
				for(var i = 0; i < data.length; i++){
					//先创建好select里面的option元素
					var option = document.createElement("option");
					//转换DOM对象为JQ对象,好用JQ里面提供的方法 给option的value赋值
					$(option).val(data[i].value);
					//给option的text赋值,这就是你点开下拉框能够看到的东西
					$(option).text(data[i].text);
					//获取select 下拉框对象,并将option添加进select
					$('#selectList').append(option);
				}
			}
		},
		error:function(){
			J.alert('Error loading Json document');
		}
	});
}



function getQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	var r = location.search.substr(1).match(reg);
	if(r != null)
		return unescape(r[2]);
	return null;
}